package cn.kais.drawer.bean;

import com.alibaba.fastjson.JSONObject;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.Map;
import java.util.Objects;

/**
 * 模板总实体
 */
public class BeanBoard {

    /**
     * 入参
     */
    public int width;
    public int height;

    public String templateGroup;

    public JSONObject current; // 视图 当前价格试图Json

    /**
     * 出参
     */
    public BufferedImage view;

    public BeanBoard(JSONObject content) {
        this.width = content.getInteger("width");
        this.height = content.getInteger("height");
        this.templateGroup = content.getString("templateGroup");
        this.current = content.getJSONObject("current");
        // 背景图 背景颜色 主要用 当前价格里的内容，所以最外层就不设置了
//        JSONArray pages = content.getJSONArray("pages");
//        this.current = pages.getJSONObject(0);

        this.view = this.create(null); // 创建底板
    }

    public BeanBoard(JSONObject content, Map<String, BufferedImage> bis) {
        this.width = content.getInteger("width");
        this.height = content.getInteger("height");
        this.current = content.getJSONObject("current");

        this.view = this.create(bis); // 创建底板

    }

    /**
     * 绘制底板
     */
    private BufferedImage create(Map<String, BufferedImage> bis) {
        long start = System.currentTimeMillis();
        BufferedImage ima;
        Graphics2D sd;
        if (!Objects.equals(this.templateGroup, "3")) {
            ima = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
            sd = ima.createGraphics();
            sd.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        } else {
            ima = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
            sd = ima.createGraphics();
        }

        // Json 视图信息直接丢给对应的实体对象
        new BeanCurrent(sd, this.current, this.width, this.height, bis);

        sd.dispose();
        long end = System.currentTimeMillis();
        System.out.println("绘图总时长：" + (end - start));
        return ima;
    }

}
